const express = require('express');
const operation = require('../controller/operation')
const router = express.Router();

// 获取评论数据
router.get('/getInfo',(req,res)=>{
    let sql = `SELECT * FROM comment,user,stadium where comment.uid = user.uid and comment.sid = stadium.sid`
       operation.execute(sql,null).then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'获取评论信息成功'
        })
       },err=>{
        res.json({
            status:400,
            msg:'获取评论信息失败'
        })
       })
})
// 获取评论数据(分页)
router.get('/pagination',(req,res)=>{
    let pagination=req.query;
    console.log(pagination);
    let currentPage = pagination.currentPage||1;
    let pageSize = pagination.pageSize||6;
    let offset = (currentPage-1)*pageSize;
    let sql =`SELECT * FROM comment,user,stadium where comment.uid = user.uid and comment.sid = stadium.sid limit ${pageSize} offset ${offset}`;
    Promise.all([operation.getTotal('comment'),operation.execute(sql,null)]).then(values=>{
        res.json({
            status:200,
            data:{ pagination:{currentPage,pageSize,total:values[0][0].total},data:values[1]},
            msg:'获取评论信息成功'  
        })
    })
})

// 添加评论
router.post('/add',(req,res)=>{
    let data ={content,uid,sid} = req.body;
    console.log(data);
    let arr =[data.uid,data.sid,data.content]
    let sql = `insert into comment(uid,sid,content) values(?,?,?)`;
       operation.execute(sql,arr).then(result=>{
        res.json({
            status:201,
            data:result,
            msg:'评论添加成功'
        })
       },err=>{
        res.json({
            status:400,
            msg:'评论添加失败'
        })
       })
})
// 删除评论
router.delete('/remove',(req,res)=>{
    let comId = req.query.comId;
    operation.remove(comId,'comId','comment').then(ok=>{
        res.json({
            status:200,
            msg:'删除成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'删除失败'
        })
    })
})
// 评论模糊查询
router.get('/search',(req,res)=>{
    let pload = req.query;
    let query = pload.query;
    let currentPage = pload.currentPage||1;
    let pageSize = pload.pageSize||6;
    let offset = (currentPage-1)*pageSize;
    let sql =`SELECT * FROM comment,user,stadium where (comment.uid = user.uid and comment.sid = stadium.sid) and (comment.comId LIKE '%${query}%' 
    or comment.uid LIKE '%${query}%'
    or comment.content LIKE '%${query}%'
    or comment.sid LIKE '%${query}%'
    or stadium.sname LIKE '${query}%'
    or user.nickName LIKE '${query}%')`;
    let pageString=`limit ${pageSize} offset ${offset}`;
    Promise.all([operation.execute(sql,null),operation.execute(sql+pageString,null)]).then(values=>{
        res.json({
            status:200,
            data:{ pagination:{currentPage,pageSize,total:values[0].length},data:values[1]},
            msg:'查询到相关评论信息'  
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'没有找到相关评论！'  
        })
    })

})
module.exports = router;